package org.fly.service.impl;

import org.fly.bean.Customer;
import org.fly.bean.CustomerLevel;
import org.fly.bean.SCLR;
import org.fly.bean.Supplier;
import org.fly.bean.query.SCLRQuery;
import org.fly.bean.warpper.SCLRWarpper;
import org.fly.dao.CustomerDao;
import org.fly.dao.CustomerLevelDao;
import org.fly.dao.SCLRDao;
import org.fly.dao.SupplierDao;
import org.fly.service.SCLRService;
import org.fly.utils.UUIDFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.fly.bean.common.BusinessException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Created by autoCode on 2018-02-19 21:08:45.
*/
@Service
public class SCLRServiceImpl implements SCLRService {

    @Autowired
    SCLRDao sclrDao;
    @Autowired
    SupplierDao supplierDao;
    @Autowired
    CustomerDao customerDao;
    @Autowired
    CustomerLevelDao customerLevelDao;


    @Override
    public List<SCLR> list(SCLRQuery query) {
        return sclrDao.list(query);
    }

    /**
     * 查询客户等级列表分
     * @param query
     * @return
     */
    @Override
    public List<SCLRWarpper> listWarpper(SCLRQuery query) {
        List<SCLR>  list = sclrDao.list(query);
        if(!list.isEmpty()){
            List<String> supplierIdList = new ArrayList<>();
            List<String> customerIdList = new ArrayList<>();
            List<String> customerLevelIdList =  new ArrayList<>();
            list.forEach(e->{
                if(!supplierIdList.contains(e.getSupplierId())){
                    supplierIdList.add(e.getSupplierId());
                }
                if(!customerIdList.contains(e.getCustomerId())){
                    customerIdList.add(e.getCustomerId());
                }
                if(!customerLevelIdList.contains(e.getCustomerLevelId())){
                    customerLevelIdList.add(e.getCustomerLevelId());
                }

            });
            List<Supplier>  suppliers = supplierDao.listByIds(supplierIdList);
            List<Customer> customers =  customerDao.listByIds(customerIdList);
            List<CustomerLevel>  customerLevels = customerLevelDao.listByIds(customerLevelIdList);
            Map<String,Supplier> supplierMap= suppliers.stream().collect(Collectors.toMap(e->e.getId(), e->e));
            Map<String ,Customer> customerMap = customers.stream().collect(Collectors.toMap(e->e.getId(),e->e));
            Map<String,CustomerLevel> customerLevelMap = customerLevels.stream().
                    collect(Collectors.toMap(e->e.getId(),e->e));
            List<SCLRWarpper> warppers = list.stream().map(e->{
                SCLRWarpper warpper = new SCLRWarpper();
                BeanUtils.copyProperties(e,warpper);
                Supplier supplier = supplierMap.get(e.getSupplierId());
                Customer customer = customerMap.get(e.getCustomerId());
                CustomerLevel customerLevel =  customerLevelMap.get(e.getCustomerLevelId());
                warpper.setSupplier(supplier);
                warpper.setCustomer(customer);
                warpper.setCustomerLevel(customerLevel);
                return warpper;
            }).collect(Collectors.toList());
            return warppers;


        }

        return null;
    }

    @Override
    public long count(SCLRQuery query) {
        return sclrDao.count(query);
    }

    @Override
    public SCLR findById(String id) {
        return sclrDao.findById(id);
    }


    @Override
    public void save(SCLR bean)  throws BusinessException {

    if(StringUtils.isNotBlank(bean.getId())){
        sclrDao.update(bean);
    }else {
        bean.setId(UUIDFactory.random());
        sclrDao.insert(bean);
    }
    }

    @Override
    public void delete(String id) {
        sclrDao.delete(id);
    }
}
